﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Jzy.Workflow.Code
{
    /// <summary>
    /// 暂不用
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class MyList<T> where T : class
    {
        private const int CAPACITY_SEED = 12;
        private int _count;

        private T[] _values;

        public int Count
        {
            get
            {
                return _count;
            }
        }

        public MyList()
        {
            _values = new T[12];
            _count = 0;
        }

        public MyList(int capacity)
        {
            _values = new T[capacity];
            _count = 0;
        }

        public void Add(T value)
        {
            if (_count == _values.Length)
            {
                ExpandCapacity();
            }
            _values[_count] = value;
            _count++;
        }

        private void ExpandCapacity()
        {
            T[] tmp = new T[_count + CAPACITY_SEED];
            for (int i = 0; i < _values.Length; i++)
            {
                tmp[i] = _values[i];
            }
            _values = tmp;
        }

        public void Insert(int index, T value)
        {
            if (_count == _values.Length)
            {
                ExpandCapacity();
            }
            for (int i = _count; i > index; i--)
            {
                _values[i] = _values[i - 1];
            }
            _values[index] = value;
            _count++;
        }

        public void Remove(T value)
        {
            for (int i = 0; i < _count; i++)
            {
                if (_values[i] == value)
                {
                    for (int j = i; j < _count-1; j++)
                    {
                        _values[j] = _values[j + 1];
                    }
                    _values[_count - 1] = null;
                    _count--;
                    break;
                }
            }
        }

        public void RemoveAt(int index)
        {
            if (index < _count)
            {
                for (int i = index; i < _count - 1; i++)
                {
                    _values[i] = _values[i + 1];
                }
                _values[_count - 1] = null;
                _count--;
            }
        }

        public void Clear()
        {
            for (int i = 0; i < _values.Length; i++)
            {
                _values[i] = null;
            }
            _count = 0;
        }

        public T this[int index]
        {
            get
            {
                if (index < _count)
                {
                    return _values[index];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (index < _count)
                {
                    _values[index] = value;
                }
                else
                {
                    throw new Exception("out of range");
                }
            }
        }
    }
}
